﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.Distribution;

namespace FwProject.Manager
{
    public class DistributionInfoManager: IDomainService
    {
        private readonly IRepository<DistributionInfo, Guid> _distributionRepository;
        public DistributionInfoManager(IRepository<DistributionInfo, Guid> distributionRepository)
        {
            _distributionRepository = distributionRepository;

        }
        /// <summary>
        /// 创建一个配送订单
        /// </summary>
        /// <param name="distribution"></param>
        /// <returns></returns>
        public async Task<Guid> CreateDistribution(DistributionInfo distribution)
        {

            var model = _distributionRepository.GetAll().Where(o => o.Id == distribution.Id || (o.LoadCarId== distribution.LoadCarId && o.Restaurantid==distribution.Restaurantid)).FirstOrDefault();
            if (model != null)
                throw new UserFriendlyException("当前配送订单已存在");

            return await _distributionRepository.InsertAndGetIdAsync(distribution);
        }
        /// <summary>
        ///修改一个配送订单
        /// </summary>
        /// <param name="distribution"></param>
        /// <returns></returns>
        public async Task UpdateDistribution(DistributionInfo distribution)
        {

            await _distributionRepository.UpdateAsync(distribution);
        }
        /// <summary>
        ///  delete 一个配送订单
        /// </summary>
        /// <param name="distribution"></param>
        /// <returns></returns>
        public async Task DeleteDistributionInfo(DistributionInfo distribution)
        {

            await _distributionRepository.DeleteAsync(distribution);
        }
    }
}
